import { ArchiveAttachments } from './archiveAttachment';
let _ArchiveAttachments = ArchiveAttachments;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import ArchiveAttachmentMock from './mdp/arc/archiveAttachmentMock';
 * ArchiveAttachmentMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mdp/arc/archiveAttachment/listKey').reply(config => {
      let params = config.params;
      let mockArchiveAttachments = _ArchiveAttachments.filter(archiveAttachment => {
      	if(params.id||params.id==''){ if( archiveAttachment.id.indexOf(params.id)>=0) return true;}
      	if(params.name||params.name==''){ if( archiveAttachment.name.indexOf(params.name)>=0) return true;}
      	if(params.url||params.url==''){ if( archiveAttachment.url.indexOf(params.url)>=0) return true;}
      	if(params.relativePath||params.relativePath==''){ if( archiveAttachment.relativePath.indexOf(params.relativePath)>=0) return true;}
      	if(params.fileSuffix||params.fileSuffix==''){ if( archiveAttachment.fileSuffix.indexOf(params.fileSuffix)>=0) return true;}
      	if(params.cdnUrl||params.cdnUrl==''){ if( archiveAttachment.cdnUrl.indexOf(params.cdnUrl)>=0) return true;}
      	if(params.isImg||params.isImg==''){ if( archiveAttachment.isImg.indexOf(params.isImg)>=0) return true;}
      	if(params.archiveId||params.archiveId==''){ if( archiveAttachment.archiveId.indexOf(params.archiveId)>=0) return true;}
      	if(params.isCdn||params.isCdn==''){ if( archiveAttachment.isCdn.indexOf(params.isCdn)>=0) return true;}
      	if(params.rootPath||params.rootPath==''){ if( archiveAttachment.rootPath.indexOf(params.rootPath)>=0) return true;}
      	if(params.createDate||params.createDate==''){ if( archiveAttachment.createDate.indexOf(params.createDate)>=0) return true;}
      	if(params.canDown||params.canDown==''){ if( archiveAttachment.canDown.indexOf(params.canDown)>=0) return true;}
      	if(params.canDel||params.canDel==''){ if( archiveAttachment.canDel.indexOf(params.canDel)>=0) return true;}
      	if(params.canRead||params.canRead==''){ if( archiveAttachment.canRead.indexOf(params.canRead)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockArchiveAttachments
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mdp/arc/archiveAttachment/list').reply(config => {
      let params = config.params;
      let mockArchiveAttachments = _ArchiveAttachments.filter(archiveAttachment => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(archiveAttachment.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(archiveAttachment.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.url||params.url==''){if(archiveAttachment.url.indexOf(params.url)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.relativePath||params.relativePath==''){if(archiveAttachment.relativePath.indexOf(params.relativePath)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.fileSuffix||params.fileSuffix==''){if(archiveAttachment.fileSuffix.indexOf(params.fileSuffix)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.cdnUrl||params.cdnUrl==''){if(archiveAttachment.cdnUrl.indexOf(params.cdnUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isImg||params.isImg==''){if(archiveAttachment.isImg.indexOf(params.isImg)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.archiveId||params.archiveId==''){if(archiveAttachment.archiveId.indexOf(params.archiveId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isCdn||params.isCdn==''){if(archiveAttachment.isCdn.indexOf(params.isCdn)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.rootPath||params.rootPath==''){if(archiveAttachment.rootPath.indexOf(params.rootPath)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.createDate||params.createDate==''){if(archiveAttachment.createDate.indexOf(params.createDate)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.canDown||params.canDown==''){if(archiveAttachment.canDown.indexOf(params.canDown)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.canDel||params.canDel==''){if(archiveAttachment.canDel.indexOf(params.canDel)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.canRead||params.canRead==''){if(archiveAttachment.canRead.indexOf(params.canRead)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockArchiveAttachments
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockArchiveAttachments.length;
      mockArchiveAttachments = mockArchiveAttachments.filter((archiveAttachment, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockArchiveAttachments,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mdp/arc/archiveAttachment/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _ArchiveAttachments = _ArchiveAttachments.filter(archiveAttachment => archiveAttachment.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mdp/arc/archiveAttachment/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _ArchiveAttachments = _ArchiveAttachments.filter(archiveAttachment => !params.includes(archiveAttachment.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mdp/arc/archiveAttachment/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,url,relativePath,fileSuffix,cdnUrl,isImg,archiveId,isCdn,rootPath,createDate,canDown,canDel,canRead } = params;
      _ArchiveAttachments.some(archiveAttachment => {
        if (archiveAttachment.id === id) {
			archiveAttachment.id = id;
			archiveAttachment.name = name;
			archiveAttachment.url = url;
			archiveAttachment.relativePath = relativePath;
			archiveAttachment.fileSuffix = fileSuffix;
			archiveAttachment.cdnUrl = cdnUrl;
			archiveAttachment.isImg = isImg;
			archiveAttachment.archiveId = archiveId;
			archiveAttachment.isCdn = isCdn;
			archiveAttachment.rootPath = rootPath;
			archiveAttachment.createDate = createDate;
			archiveAttachment.canDown = canDown;
			archiveAttachment.canDel = canDel;
			archiveAttachment.canRead = canRead
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mdp/arc/archiveAttachment/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,name,url,relativePath,fileSuffix,cdnUrl,isImg,archiveId,isCdn,rootPath,createDate,canDown,canDel,canRead } = params;
      _ArchiveAttachments.push({
		id:id,
		name:name,
		url:url,
		relativePath:relativePath,
		fileSuffix:fileSuffix,
		cdnUrl:cdnUrl,
		isImg:isImg,
		archiveId:archiveId,
		isCdn:isCdn,
		rootPath:rootPath,
		createDate:createDate,
		canDown:canDown,
		canDel:canDel,
		canRead:canRead
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};